package brightrac.database;

import java.io.DataInputStream;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.io.InputStreamReader;

import com.sun.swing.internal.plaf.basic.resources.basic;

public class DBSetup {
	
	private File file = null;
	private FileInputStream fileInStrm = null;
    private BufferedReader bufferInStrm = null;
    private String line = null;
    private String query = "";
    private Basic connectorObj;
    private Pattern pattern_strt = Pattern.compile("^#");
    private Pattern pattern_ind = Pattern.compile("CREATE INDEX|" +
    		"CREATE FULLTEXT INDEX|CREATE UNIQUE INDEX");
    private Pattern pattern_insert = Pattern.compile("^INSERT");
    private Pattern pattern_value = Pattern.compile("VALUES");
    private List<String> queryList =new ArrayList<String>();
    
    public DBSetup(Basic connectorObj)
    {
    	this.connectorObj = connectorObj;
    }
    /**
     * Load File
     * @param fileName File To be loaded
     * @param queryList 
     */
    public List <String> getShema(String fileName){
    	try{
    		file = new File(fileName);
    		fileInStrm = new FileInputStream(file);
    		bufferInStrm = new BufferedReader(new InputStreamReader(fileInStrm));
    		while ((line = bufferInStrm.readLine()) != null) {
      	      	line = line.replaceAll("\\n", "");
      	      line = line.replaceAll("\\s+", " ");
      	      if(pattern_strt.matcher(line).find() && !query.equals("") ){
      	        	queryList.add(query);
      	        	query = "";
      	      }
      	      else if (pattern_ind.matcher(line).find()){
      	    	  queryList.add(query);
      	    	  queryList.add(line);
      	    	  query = "";
      	      }
      	      else if(line.equals("") || line != null)
      	    	  query = query + " " + line;
    	}
    		bufferInStrm.close();
    	}
    	catch(IOException e){
    		e.printStackTrace();    		
    	}
    	return (queryList);
    }
    
    /**
     * Create required table
     * @param connectorObj {@link basic}
     */
    public void createTables(String fileName){
    	getShema(fileName);
    	for(int index = 1; index < queryList.size() ; index++)
    		if(!queryList.get(index).equals(" "))
    			connectorObj.runQuery(queryList.get(index));
    }
    
    
    public void insertIntialData(String fileName){
    	try{
	    	file = new File(fileName);
			fileInStrm = new FileInputStream(file);
			bufferInStrm = new BufferedReader(new InputStreamReader(fileInStrm));
			String row = "";
			while ((line = bufferInStrm.readLine()) != null){
      	      	line = line.replaceAll("\\n", "");
      	      	line = line.replaceAll("`", " ");
      	      	if(pattern_insert.matcher(line).find()){
      	      		String [] data = pattern_value.split(line);
      	      		row = data[0] + " VALUES ";
      	      		System.out.println(row);
      	      	}
      	      	else if(!line.equals("")){
      	      		System.out.println(connectorObj.runQuery(row + "(" + line + ")") + "\n");
      	      	}
      	      	
			}
			bufferInStrm.close();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    }
    
	

}
